package com.zjc.yohoshop.mvp.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.zjc.lib_core.base.BaseActivity;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.eventbus_bean.SelectCategoryRadioButton;
import com.zjc.yohoshop.eventbus_bean.GVToHABus;
import com.zjc.yohoshop.mvc.view.FragmentActivity;
import com.zjc.yohoshop.mvp.view.adapter.HomeMenuAdapter;
import com.zjc.yohoshop.bean.BaseEntity;
import com.zjc.yohoshop.bean.HomeMenuEntity;
import com.zjc.yohoshop.di.compont.DaggerHomeCompont;
import com.zjc.yohoshop.di.modules.HomeModules;
import com.zjc.yohoshop.eventbus_bean.BaseEventBusEntity;
import com.zjc.yohoshop.eventbus_bean.OpenEventBus;
import com.zjc.yohoshop.mvp.contract.HomeContract;
import com.zjc.yohoshop.mvp.persenter.HomePresenter;
import com.zjc.yohoshop.mvp.view.fragment.ClassifyFragment;
import com.zjc.yohoshop.mvp.view.fragment.HomeFragment;
import com.zjc.yohoshop.mvp.view.fragment.MineFragment;
import com.zjc.yohoshop.mvp.view.fragment.ShoesFragment;
import com.zjc.yohoshop.mvp.view.fragment.StrollFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import pl.droidsonroids.gif.GifImageView;

/**
 * 主页面
 */
public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.IHomeView, View.OnClickListener {
    private SlidingMenu slidingMenu;
    private RadioGroup rg_home_bottom;
    private RecyclerView rv_menu;
    private GifImageView giv_home_ufo;
    private FragmentManager manager;
    private HomeFragment homeFragment;
    private HomeMenuAdapter homeMenuAdapter;
    private ClassifyFragment classifyFragment;
    private ShoesFragment shoesFragment;
    private StrollFragment strollFragment;
    private MineFragment mineFragment;
    private ImageView iv_home_menu_men, iv_home_menu_women, iv_home_bottom_car;
    private RadioGroup rg_menu;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                rg_home_bottom.check(R.id.rb_home_homepage);
            }else if (msg.what == 2){
                rg_home_bottom.check(R.id.rb_home_category);
            }else if (msg.what == 3){
                rg_home_bottom.check(R.id.rb_home_mine);
            }
        }
    };

    @Override
    public int layoutID() {
        return R.layout.activity_home;
    }

    @Override
    public void init() {
        manager = getSupportFragmentManager();
        rg_home_bottom = findViewById(R.id.rg_home_bottom);
        giv_home_ufo = findViewById(R.id.giv_home_ufo);
        iv_home_bottom_car = findViewById(R.id.iv_home_bottom_car);


        homeFragment = new HomeFragment();
        classifyFragment = new ClassifyFragment();
        shoesFragment = new ShoesFragment();
        strollFragment= new StrollFragment();
        mineFragment = new MineFragment();

        initmenu();
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        p.requestMenu();
        addAll();
    }

    @Override
    public void setupDagger() {
        DaggerHomeCompont.builder()
                .homeModules(new HomeModules(this))
                .build().inject(this);
    }

    @Override
    public void success(BaseEntity baseEntity) {
        HomeMenuEntity menuEntity = (HomeMenuEntity) baseEntity;
        //在主线程->刷新UI
        //刷新适配器
        homeMenuAdapter.setNewData(menuEntity.values);

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    /**
     * 事件监听方法
     */
    @Override
    public void initListener() {
        /**
         * 主页面底部单选按钮组监听
         */
        rg_home_bottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home_homepage:
                        showFrag(homeFragment);
                        iv_home_bottom_car.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_home_category:
                        showFrag(classifyFragment);
                        iv_home_bottom_car.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_home_see:
                        showFrag(strollFragment);
                        iv_home_bottom_car.setVisibility(View.GONE);
                        break;
                    case R.id.rb_home_mine:
                        showFrag(mineFragment);
                        iv_home_bottom_car.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_home_zhong:
                        iv_home_bottom_car.setVisibility(View.VISIBLE);
                        break;

                }
            }
        });
        /**
         * 默认选中
         */
        rg_home_bottom.check(R.id.rb_home_homepage);

        /**
         * 侧滑菜单按钮组
         */
        rg_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_menu_men:
                        iv_home_menu_men.setVisibility(View.VISIBLE);
                        iv_home_menu_women.setVisibility(View.INVISIBLE);
                        break;
                    case R.id.rb_menu_women:
                        iv_home_menu_men.setVisibility(View.INVISIBLE);
                        iv_home_menu_women.setVisibility(View.VISIBLE);
                        Toast.makeText(HomeActivity.this, "还没数据", Toast.LENGTH_SHORT).show();
                        break;
                }
                slidingMenu.showContent(true);
            }
        });
        rg_menu.check(R.id.rb_menu_men);

        /**
         * 点击侧滑菜单的选项发送menu_id和关闭侧滑菜单
         */
        homeMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                showFrag(classifyFragment);
                EventBus.getDefault().post(new SelectCategoryRadioButton(BaseEventBusEntity.CATEGORYRADIO, ((HomeMenuAdapter)adapter).getData().get(position).menu_name));
                slidingMenu.showContent(true);
            }
        });
        giv_home_ufo.setOnClickListener(this);
        iv_home_bottom_car.setOnClickListener(this);

    }

    /**
     * 侧滑
     */
    private void initmenu() {
        //TODO 1:创建菜单对象
        slidingMenu = new SlidingMenu(this);
        //TODO 2：设置属性
        //SlidingMenu.RIGHT   SlidingMenu.LEFT
        slidingMenu.setMode(SlidingMenu.LEFT);//设置从哪边出来
        //TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);//不能滑动
        slidingMenu.setBehindOffset(200);//单位是像素px 侧滑菜单滑出后，界面剩余的宽度。
        View menu = LayoutInflater.from(HomeActivity.this).inflate(R.layout.layout_sliding_menu, null);
        rg_menu = menu.findViewById(R.id.rg_menu);
        rv_menu = menu.findViewById(R.id.rv_sliding_menu);
        iv_home_menu_men = menu.findViewById(R.id.iv_home_menu_men);
        iv_home_menu_women = menu.findViewById(R.id.iv_home_menu_women);
        rv_menu.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
        homeMenuAdapter = new HomeMenuAdapter(R.layout.item_home_menu);
        rv_menu.setAdapter(homeMenuAdapter);
        slidingMenu.setMenu(menu);
        slidingMenu.setShadowWidth(200);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//绑定到指定的Context
    }

    /**
     * 点击事件监听
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.giv_home_ufo:
                rg_home_bottom.check(R.id.rb_home_zhong);
                showFrag(shoesFragment);
                break;
            case R.id.iv_home_bottom_car:
                Intent goods_car = new Intent(HomeActivity.this, FragmentActivity.class);
                goods_car.putExtra("type", "goods_car");
                startActivity(goods_car);
                break;
        }
    }

    /**
     * 显示指定的fragment
     */
    private void showFrag(Fragment fragment){
        hideAll();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.show(fragment);
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏所有Fragment
     */
    private void  hideAll(){
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(homeFragment);
        transaction.hide(classifyFragment);
        transaction.hide(shoesFragment);
        transaction.hide(strollFragment);
        transaction.hide(mineFragment);
        transaction.commitAllowingStateLoss();
    }

    /**
     * 添加所有Fragment
     */
    private void addAll(){
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_home, homeFragment);
        transaction.add(R.id.fl_home, classifyFragment);
        transaction.add(R.id.fl_home, shoesFragment);
        transaction.add(R.id.fl_home, strollFragment);
        transaction.add(R.id.fl_home, mineFragment);
        transaction.commit();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openMenu(BaseEventBusEntity busEntity){
        if (busEntity instanceof  OpenEventBus){
            if (busEntity.type == BaseEventBusEntity.OPENMENU){
                slidingMenu.showMenu();
            }
        }
        if (busEntity instanceof GVToHABus){
            GVToHABus gvToHABus = (GVToHABus) busEntity;
            if ("首页".equals(gvToHABus.title)){
                handler.sendEmptyMessage(1);
            }else if ("我的".equals(gvToHABus.title)){
                handler.sendEmptyMessage(3);
            }else if ("分类".equals(gvToHABus.title)){
                handler.sendEmptyMessage(2);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (100==requestCode&&RESULT_OK == resultCode){
            Bundle bund = data.getExtras();
            String result = bund.getString("result");
            Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        }
    }



}
